package me.dwliu.ebase.modules.gen.config;

import lombok.extern.slf4j.Slf4j;
import me.dwliu.ebase.modules.gen.exception.CustomException;
import me.dwliu.ebase.modules.gen.mapper.GenMapper;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 数据源持久层配置
 *
 * @author liudw
 * @date 2018/10/6 21:55
 **/
@Configuration
@MapperScan("me.dwliu.ebase.modules.gen.mapper")
@Slf4j
public class DbMapperConfig {
    @Autowired
    private GenMapper mySqlGenMapper;
    @Autowired
    private GenMapper oracleGenMapper;
    @Autowired
    private GenMapper sqlServerGenMapper;
    @Autowired
    private GenMapper postgreSQLGenMapper;

    @Value("${spring.datasource.driverClassName}")
    private String driverClassName;


    @Bean
    //@ConditionalOnProperty(prefix = "spring.datasource", name = "driverClassName", havingValue = "com.mysql.jdbc.Driver1")
    public GenMapper genMapper() {
        log.info("------{}-------", driverClassName);

        if (StringUtils.equals(driverClassName, "com.mysql.jdbc.Driver")
                || StringUtils.equals(driverClassName, "com.mysql.cj.jdbc.Driver")) {
            return mySqlGenMapper;
        }

        if (StringUtils.equals(driverClassName, "oracle.jdbc.OracleDriver")) {
            return oracleGenMapper;
        }

        if (StringUtils.equals(driverClassName, "com.microsoft.sqlserver.jdbc.SQLServerDriver")) {
            return sqlServerGenMapper;
        }

        if (StringUtils.equals(driverClassName, "org.postgresql.Driver")) {
            return postgreSQLGenMapper;
        }

        throw new CustomException("不支持的数据库类型");

    }


}
